let pi = 4. *. atan 1.

let min_array = fun a ->
  let v = ref a.(0) in
    for i = 1 to Array.length a - 1 do
      v := min a.(i) !v;
    done;
    !v

let max_array = fun a ->
  let v = ref a.(0) in
    for i = 1 to Array.length a - 1 do
      v := max a.(i) !v;
    done;
    !v

let arg_max = fun a ->
  let id = ref 0 in
    for i = 1 to Array.length a - 1 do
      if a.(i) > a.(!id) then id := i;
    done;
    !id

let arg_min = fun a ->
  let id = ref 0 in
    for i = 1 to Array.length a - 1 do
      if a.(i) < a.(!id) then id := i;
    done;
    !id

exception Elt_found

let random_in_list = fun l ->
  let n = List.length l in
  List.nth l (Random.int n)

(* returns position of element e in a *)
let position_in_array = fun e a ->
  let p = ref (-1) in
    try
      Array.iteri 
	(fun i elt -> if elt=e then begin p:=i; raise Elt_found end;)
	a;
      raise Not_found
    with Elt_found -> !p


(* round *)
let round = fun f ->
  if abs_float (ceil f -. f) > abs_float (floor f -. f)
  then
    if f> 0.
    then int_of_float f
    else int_of_float f -1
  else 
    if f > 0.
    then int_of_float f + 1
    else int_of_float f
    ;;
